<?php
namespace Modules\Asset\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Asset\Entities\ScoreSku;

use Validator;
use ThrowException;
use DB;

class ScoreSkuController extends Controller
{
    // 获取开奖列表
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        // $class = @config('activity.map')[$activityType] ?: ThrowException::Conflict('无效的活动类型');

        $options = $req->all();
        $options['order_by'] = ['list_weight' => 'desc'];

        $list = ScoreSku::getList($options);

        return $list->generateListResponse();
    }

    public function store(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $info = $req->all();

        $item = ScoreSku::create($info);

        return [
            'id' => $item->id,
            'uuid' => $item->uuid
        ];
    }

    public function show(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = ScoreSku::getEntity([
            'uuid' => $uuid
        ]);

        $info = $item->getInfo();

        return [
            'info' => $info
        ];
    }

    public function update(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = ScoreSku::getEntity([
            'uuid' => $uuid
        ]);

        if ($req->type === 'update') {
            $info = $req->input('attributes');
            $item->update($info);
        }
        elseif ($req->type  === 'set_top')  {
            // 置顶
            $item->update([
                'set_top_at' => now()
            ]);
        }
        elseif ($req->type  === 'unset_top')  {
            // 取消置顶
            $item->update([
                'set_top_at' => NULL
            ]);
        }

        return [];
    }

    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = ScoreSku::getEntity([
            'uuid' => $uuid
        ]);

        $item->delete();

        return [];
    }
}
